//导演类，控制游戏的逻辑
import Instance from './base/Instance.js'
import DataStore from './base/DataStore.js'
import UpPencil from './runtime/UpPencil.js'
import DownPencil from './runtime/DownPencil.js'

export default class extends Instance {
  // static const moveSpeed = 2
  constructor() {
    super()
    this.dataStore = DataStore.getInstance()
    this.moveSpeed = 2
    this.isGameOver = false
  }

  createPencils() {
    const minTop = DataStore.getInstance().canvas.height / 8
    const maxTop = DataStore.getInstance().canvas.height / 2
    const top = minTop + Math.random() * (maxTop - minTop)
    this.dataStore.get('pencils').push(new UpPencil(top), new DownPencil(top))
  }
  drawPencils() {
    const pencils = this.dataStore.get('pencils')
    const score = this.dataStore.get('score')
    if (pencils[0].x + pencils[0].width < 0 && pencils.length === 4) {
      pencils.splice(0, 2)
      score.isScore = true
    }
    if (pencils[0].x <= DataStore.getInstance().canvas.width - pencils[0].x / 2 - pencils[0].width * 2 &&
      pencils.length === 2) {
      this.createPencils()
    }
    pencils.forEach(item => item.draw())
  }
  birdsEvent() {
    this.dataStore.get('birds').birdsY.forEach((item, index) => {
      this.dataStore.get('birds').y[index] = item
    })
    this.dataStore.get('birds').time = 0
  }
  check() {
    const birds = this.dataStore.get('birds')
    const land = this.dataStore.get('land')
    const pencils = this.dataStore.get('pencils')
    const score = this.dataStore.get('score')
    // if (birds.birdsY[0] + birds.birdsHeight[0] > land.y) {
    //   console.log('撞击地板了')
    //   this.isGameOver = true
    // }
    // 地板碰撞逻辑 // 天花板碰撞逻辑
    this.isGameOver = birds.birdsY[0] + birds.birdsHeight[0] >= land.y || birds.birdsY[0] < 0
    // 铅笔碰撞逻辑
    const birdsBorder = {
      top: birds.birdsY[0],
      bottom: birds.birdsHeight[0] + birds.birdsY[0],
      left: birds.birdsX[0],
      right: birds.birdsWidth[0] + birds.birdsX[0]
    }
    pencils.forEach((item, index) => {
      const pencilBorder = {
        top: item.y,
        bottom: item.y + item.height,
        left: item.x,
        right: item.x + item.width
      }
      if (this.isStirke(birdsBorder, pencilBorder)) {
        this.isGameOver = true
      }
    })
    // 评分逻辑
    if (birds.birdsX[0] > pencils[0].x + pencils[0].width && score.isScore) {
      wx.vibrateShort()
      score.isScore = false
      score.scoreNumber++
    }
  }
  isStirke(bird, pencil) {
    return !(bird.top > pencil.bottom || bird.bottom < pencil.top || bird.left > pencil.right || bird.right < pencil.left)
  }
  run() {
    this.check()
    if (this.isGameOver) {
      console.log('游戏结束!')
      cancelAnimationFrame(this.dataStore.get('timer'))
      this.dataStore.get('startButton').draw()
      this.dataStore.bgm.pause()
      wx.triggerGC()
      return
    }
    const backgroundSprite = this.dataStore.get('background')
    const landSprite = this.dataStore.get('land')
    const birds = this.dataStore.get('birds')
    backgroundSprite.draw()
    this.drawPencils()
    landSprite.draw()
    this.dataStore.get('score').draw()
    birds.draw()
    const timer = requestAnimationFrame(() => this.run())
    this.dataStore.put('timer', timer)
  }
}
